/**
 * 
 */
package etric.mapmaker.tools;

/**
 * This class holds functions that convert lat/long into X/Y and vice versa.
 * Code was taken from the tiles@home project (tilesGen.pl)
 * @author etric
 */
public class CoordsConvert
{
	/** map projection as descriped on the Developer site on OSM */
	private static final double limitYright =  3.141586830940926;
	//private static final double limitYleft  = -3.1415868309409065;
	private static final double rangeY      =  6.283173661881833;
	
	/**
	 * Converts X value to it's longitude 
	 * @param x		// the X value
	 * @param zoom	// the specifiec zoom
	 * @return		// longitude value
	 */
	public static double convXtoLong(int x, int zoom)
	{
		double xLong = -180 + (x * (360/(Math.pow(2, zoom))));
			
		return xLong;
	}
	
	/**
	 * Converts Y value to it's latitute
	 * @param y		// the Y value
	 * @param zoom	// the specific zoom
	 * @return		// latitude value
	 */
	public static double convYtoLat(int y, int zoom)
	{
		double yLat = 0;
		
		yLat = (1 / (Math.pow(2, zoom))) * y;
		
		yLat = limitYright - rangeY * yLat;
		
		yLat = Math.toDegrees(Math.atan(Math.sinh(yLat)));
					
		return yLat;
	}
	
	/**
	 * Converts decimal values into degree° minutes' seeconds 
	 * @param 	// decValue
	 * @return	// String representing the value
	 */
	public static String convToDegMinSec(double decValue)
	{
		int deg    = (int)decValue;	//cuts fractional digits
		int min    = (int)(60*(decValue-deg));
		double sec = 60 * ((60*(decValue-deg)) - (int)(60*(decValue-deg)));
		
		int secRound = (int)(sec*100);
		sec = ((double)secRound)/100;
		
		return deg + "°" + min + "'" + sec;
	}

}
